package com.cefa.finance.service;

import com.cefa.finance.dao.*;
import com.cefa.finance.model.*;
import com.cefa.finance.result.ResultModel;
import com.cefa.finance.result.ResultStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class VehicleInfoService {

    private final static Logger logger = LoggerFactory.getLogger(VehicleInfoService.class);

    @Autowired
    private MaintenanceRecordRepository maintenanceRecordRepository;

    @Autowired
    private AccidentRecordRepository accidentRecordRepository;

    @Autowired
    private ContractRepository contractRepository;

    @Autowired
    private ContractMapper contractMapper;

    @Transactional("transactionManagerPrimary")
    public ResultModel addMaintenanceRecord(MaintenanceRecord maintenanceRecord) {
        if (checkIsExpired(maintenanceRecord.getContractId(),maintenanceRecord.getMaintenanceDate().getTime())){
            return ResultModel.ERROR(ResultStatus.OUTOFDATE);
        }
        MaintenanceRecord record = maintenanceRecordRepository.save(maintenanceRecord);
        return ResultModel.OK(record);
    }

    @Transactional("transactionManagerPrimary")
    public ResultModel deleteMaintenanceRecord(MaintenanceRecord maintenanceRecord) {
        maintenanceRecordRepository.delete(maintenanceRecord.getId());
        return ResultModel.OK();
    }

    @Transactional("transactionManagerPrimary")
    public ResultModel addAccidentRecord(AccidentRecord accidentRecord) {
        if (checkIsExpired(accidentRecord.getContractId(),accidentRecord.getAccidentDate().getTime())){
            return ResultModel.ERROR(ResultStatus.OUTOFDATE);
        }
        AccidentRecord record = accidentRecordRepository.save(accidentRecord);
        return ResultModel.OK(record);
    }

    @Transactional("transactionManagerPrimary")
    public ResultModel deleteaccidentRecord(AccidentRecord accidentRecord) {
        accidentRecordRepository.delete(accidentRecord.getId());
        return ResultModel.OK();
    }

    @Transactional("transactionManagerPrimary")
    public ResultModel updatetravlledKms(Contract source) {
        Contract one = contractRepository.findOne(source.getId());
        one.setTravlledKilometers(source.getTravlledKilometers());
        Contract save = contractRepository.save(one);
        return ResultModel.OK(save);
    }

    public ResultModel listVehicleInfo(String searchText, Integer pageNo, Integer pageCount) {

        ArrayList<ContractModel> allResult = new ArrayList<>();
        ArrayList<ContractModel> certainContracts = new ArrayList<>();
        HashMap<String, Object> resultMap = new HashMap<>();

        List<ContractModel> allContracts = contractMapper.findAllVehicleContracts();

        for (ContractModel contract : allContracts) {
            String a;
            allResult.add(contract);
            if (searchText != null && !"".equals(searchText) && contract.getTenantryName() != null && contract.getVin() != null && contract.getPhone() != null) {
                if (contract.getTenantryName().contains(searchText) || contract.getVin().contains(searchText)) {
                    certainContracts.add(contract);
                }
            }
        }

        if (searchText != null && !"".equals(searchText)) {
            allResult = certainContracts;
        }

        if (pageNo != null && pageCount != null) {
            resultMap.put("totalPage", (allResult.size() - 1) / pageCount + 1);
            allResult = new ArrayList<>(allResult.subList((pageNo - 1) * pageCount, pageCount * pageNo > allResult.size() ? allResult.size() : pageCount * pageNo));
        }
        resultMap.put("contracts", allResult);
        return ResultModel.OK(resultMap);
    }

    public Boolean checkIsExpired(Long contractId,Long MaintenanceDate){
        Contract one = contractRepository.findOne(contractId);
        return  (MaintenanceDate>=(one.getEndDateOfLease().getTime()+86400000) ||
                MaintenanceDate<one.getStartDateOfLease().getTime());
    }
    
}


















